package com.exartisan.molisasurvey;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.exartisan.bean.User;
import com.exartisan.datamanager.SurveyManager;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceManager;

public abstract class BaseActivity extends SherlockActivity implements OnSharedPreferenceChangeListener {
	protected static boolean didStart = false;
	// saving setting variables
	public static int checkMethod;
	public static String fontName;
	// data of the application below

	private boolean settingsChanged = false;
	private boolean willHotUpdateSetting = false;
	private static User user;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getSettings();
		if (didStart == false) {
			didStart = true;
			doPreparationProcessOfApp();
		}
		ActivityHolder.allActivities.add(this);
		setUpActionBar();
		displayActivity();
		initVariables();
		initViews();
		initActions();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if(willHotUpdateSetting) if (settingsChanged) updateSettings();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		final com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.splash, menu);
		return true;
	}

	/**
	 * SetContentView, display setting and so on..
	 */
	protected abstract void displayActivity();

	/**
	 * initialize Views
	 */
	protected abstract void initViews();

	/**
	 * initialize global variables
	 */
	protected abstract void initVariables();

	/**
	 * beginning actions of the Activity
	 */
	protected abstract void initActions();

	/**
	 * Scan files from SDcard then read content of those.</br> Finally, save
	 * result of those processes into some collections.</br> Those are data of
	 * the application. Thus, write once, use the data everywhere.
	 */
	
	protected void setUpActionBar() {
		
	}
	
	private void doPreparationProcessOfApp() {
		//SurveyManager.getInstance().addSurveyFromAssets(getApplicationContext(), "phan1.xml");
		user = new User();
		user.setData(null);
	}
	

	private void getSettings() {
		SharedPreferences preferences = PreferenceManager
				.getDefaultSharedPreferences(this);
		preferences.registerOnSharedPreferenceChangeListener(this);
		checkMethod = Integer.valueOf(preferences.getString(
				"check_method_list", "0"));
		fontName = preferences.getString("font_list", "default");
	}
	
	private void restartActivity(){
		Intent thisIntent = getIntent();
		finish();
		startActivity(thisIntent);
	}
	
	private void updateSettings(){
		AlertDialog.Builder restartAlertBuilder = new AlertDialog.Builder(this);
		restartAlertBuilder.setTitle("Confirm Relaunch");
		restartAlertBuilder.setMessage("You have change some settings that need this test to relaunch to take effect.\nDo you want to relaunch ?");
		restartAlertBuilder.setCancelable(false);
		restartAlertBuilder.setPositiveButton("Ok", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				restartActivity();
				
			}
		});
		restartAlertBuilder.setNegativeButton("Nope", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
				settingsChanged = false;
			}
		});
		
		AlertDialog restartAlertDialog = restartAlertBuilder.create();
		restartAlertDialog.show();
	}
	
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		settingsChanged = true;
		
	}
	
	protected void setWillHotUpdateSettings(boolean willHotUpdateSettings) {
		this.willHotUpdateSetting = willHotUpdateSettings;
	}

	public static User getUser() {
		return user;
	}

	public static void setUser(User user) {
		BaseActivity.user = user;
	}

}
